package com.jianjun.project.system.post;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jianjun.common.constant.UserConstants;
import com.jianjun.common.exception.ServiceException;
import com.jianjun.project.common.model.SysPost;
import com.jianjun.project.common.model.SysUserPost;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 * @author source
 * {@code @date} 2025/1/14 13:47
 */
public class SysPostService {
    private final SysPost dao = new SysPost().dao();


    public Page<SysPost> paginate(SysPost model) {
        SqlPara sqlParaDb = Db.getSqlPara("querySysPost", Kv.by("cond", model.toMap()).set(model.getColumnAsc()));
        return dao.paginate(model.getPageNum(), model.getPageSize(), sqlParaDb);
    }

    /**
     * 根据条件分页查询字典类型
     */
    public List<SysPost> selectPostList(Model<SysPost> model) {
        SqlPara sqlParaDb = Db.getSqlPara("querySysPost", Kv.by("cond", model.toMap()));
        return dao.find(sqlParaDb);
    }


    /**
     * 校验岗位名称是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    public boolean checkPostNameUnique(SysPost post) {
        long postId = Objects.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost model = new SysPost();
        model.setPostName(post.getPostName());
        SqlPara sqlParaDb = Db.getSqlPara("querySysPost", Kv.by("cond", model.toMap()));
        SysPost info = dao.findFirst(sqlParaDb);
        if (Objects.nonNull(info) && info.getPostId() != postId) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    public boolean checkPostCodeUnique(SysPost post) {
        long postId = Objects.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost model = new SysPost();
        model.setPostCode(post.getPostCode());
        SqlPara sqlParaDb = Db.getSqlPara("querySysPost", Kv.by("cond", model.toMap()));
        SysPost info = dao.findFirst(sqlParaDb);
        if (Objects.nonNull(info) && info.getPostId() != postId) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    public int countUserPostById(Long postId) {
        SysUserPost model = new SysUserPost();
        model.setPostId(postId);
        SqlPara sqlParaDb = Db.getSqlPara("querySysUserPost", Kv.by("cond", model.toMap()));
        return Db.queryInt(sqlParaDb.getSql().replaceAll("(?<=select ).*\\*", "count(1)"), sqlParaDb.getPara());
    }

    public SysPost selectPostById(Long postId) {
        return dao.findById(postId);
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     * @return 结果
     */
    @Before(Tx.class)
    public boolean deletePostByIds(Long[] postIds) {
        for (Long postId : postIds) {
            SysPost post = selectPostById(postId);
            if (countUserPostById(postId) > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
            dao.deleteById(postId);
        }
        return true;
    }

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    public List<SysPost> selectPostAll() {
        return dao.findAll();
    }

    /**
     * 新增保存岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    public boolean insertPost(SysPost post) {
        post.setCreateTime(new Date());
        return post.save();
    }

    /**
     * 修改保存岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    public boolean updatePost(SysPost post) {
        post.setUpdateTime(new Date());
        return post.update();
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */

    public List<Long> selectPostIdListByUserId(Long userId) {
        List<SysPost> list = selectPostListByUserId(userId);
        return list.stream().map(SysPost::getPostId).collect(Collectors.toList());
    }

    /**
     * 根据用户ID获取岗位列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */

    public List<SysPost> selectPostListByUserId(Long userId) {
        SqlPara sqlParaDb = Db.getSqlPara("querySysPost", Kv.by("userId", userId));
        return dao.find(sqlParaDb);
    }
}
